import {reqGetUserList, reqGetUserListInfo} from "@/api";

let state = {
    // 用户列表['userName', 'userId']
    userList:{},
    // 用户信息列表
    userInfoList:[],
}
let actions = {
    // 获取用户列表
    async getUserList({commit}){
        let result = await reqGetUserList();
        if (result.code == 200){
            commit('GETUSERLIST', result.data);
        }else{
            this._vm.$message({
                showClose:true,
                message:'获取userData---getUserList数据失败',
                type:"error"
            })
        }
    },
    async getUserInfoList({commit}, arr){
        let result = await reqGetUserListInfo(arr);
        if (result.code==200){
            commit('GETUSERINFOLIST', result.data);
        }
    }
}
let mutations = {
    GETUSERLIST(state, data){
        state.userList = data;
    },
    GETUSERINFOLIST(state, data){
        state.userInfoList = data;
    }
}
let getters = {
    // 返回一个用户列表[{userName, userId}]
    getUserList(){
        return Object.forEachToArr(state.userList, (item, index, key)=>{
            return  Object.assign({}, {
                value:item.userName,
                userId:item.userId
            })
        })
    },
    // 返回一个用户信息列表
    getUserInfoList(){
        return state.userInfoList;
    },
    // 返回一个用户对象{userId:userName}
    getUserNameList(){
        return Object.forEach(state.userList, (item, index, key)=>{
            return item.userName;
        }, '1')
    }
}
export default {
    state,
    actions,
    mutations,
    getters
}